package com.efounder.core;

import android.content.Context;
import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;

import com.czt.mp3recorder.MP3Recorder;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class VoiceRecorder_endWithMP3 extends VoiceRecorderAbstract {

	private static final String TAG =VoiceRecorder_endWithMP3.class.getName() ;
	private Context mContext;

	private MP3Recorder recorder;
	private boolean isRecording;
	private long startTime;
	private String voiceFilePath;
	private String voiceFileName;
	private File file;
	private Handler handler;

	/**
	 *
	 * @param handler1  通过handlemessage处理界面动态效果
     */
	public VoiceRecorder_endWithMP3(Handler handler1, Context context) {
		isRecording = false;
		voiceFilePath = null;
		mContext = context;
		voiceFileName = null;
		handler = handler1;
		
	}




	@Override
	public String startRecording(){

		return startRecording("Default");

	}

	/**
	 * 开始录音
	 * @param
	 *
     * @return
     */
	@Override
	public String startRecording(String speakerID) {
		file = null;

		try {
			if (recorder != null) {
				recorder = null;
			}


			voiceFileName = getVoiceFileName(speakerID);

			voiceFilePath = getVoiceFilePath(speakerID);
			Log.e(TAG, "startRecording: "+ voiceFilePath);
			File file0 = new File(voiceFilePath);
			if(!file0.exists()){
				file0.mkdirs();
			}
			this.file = new File(file0,voiceFileName);
			recorder = new MP3Recorder(file);

			isRecording = true;
			recorder.start();
		} catch (IOException exception) {
			exception.printStackTrace();
//			EMLog.e("voice", "prepare() failed");
			if(this.file.exists()){
				file.delete();
			}
			Toast.makeText(mContext,"录音失败" , Toast.LENGTH_SHORT).show();
		}
		/*new Thread(new Runnable() {

			public void run() {
				try {
					while (isRecording) {
						Message message = new Message();
						message.what = (recorder.getMaxAmplitude() * 13) / 32767;
						handler.sendMessage(message);
						SystemClock.sleep(100L);
					}
				} catch (Exception exception) {
					exception.printStackTrace();
//					EMLog.e("voice", exception.toString());
				}
			}

		}).start();
		startTime = (new Date()).getTime();*/
		Log.w("voice", (new StringBuilder("-------------------start voice recording to file:"))
				.append(file.getAbsolutePath()).toString());
		return file != null ? file.getAbsolutePath() : null;
	}

	/**
	 * 放弃录音
	 *
	 */
	@Override
	public void discardRecording() {
		if (recorder != null) {
			try {
				recorder.stop();
				recorder = null;
				if (file != null && file.exists() && !file.isDirectory())
					file.delete();
			} catch (IllegalStateException illegalstateexception) {
			} catch (RuntimeException runtimeexception) {
			}
			isRecording = false;
		}
	}

	/**
	 * 停止录音
	 * @return
     */
	@Override
	public int stopRecoding() {
		if (recorder != null) {
			isRecording = false;
			recorder.stop();
			recorder = null;
			if (file != null && file.exists() && file.isFile()
					&& file.length() == 0L) {
				file.delete();
				return -1011;
			} else {
				int i = (int) ((new Date()).getTime() - startTime) / 1000;
				Log.w("voice", (new StringBuilder(
						"------------voice recording finished. seconds:")).append(i)
						.append(" file length:").append(file.length())
						.toString());
				return i;
			}
		} else {
			return 0;
		}
	}

	protected void finalize() throws Throwable {
		super.finalize();
		if (recorder != null)
			recorder = null;
	}

	/**
	 * 传入id可拿到用户的录音文件名字
	 * @param s
	 * @return
     */
	@Override
	public String getVoiceFileName(String s) {
		Time time = new Time();
		time.setToNow();
		return (new StringBuilder(String.valueOf(s)))
				.append(time.toString().substring(0, 15)).append(".mp3")
				.toString();
	}

	@Override
	public boolean isRecording() {
		return isRecording;
	}



}
